#ifndef __UPGRADE_H__
#define __UPGRADE_H__

#include <libusb.h>
#include "common.h"
#include "ofprint_common.h"

#define CONF_FILE_PATH "/opt/apps/ofilm.oms.Ofprint/files/conf/"
#define FW_VERSION_FILE "/opt/apps/ofilm.oms.Ofprint/files/conf/oms_fw.conf"

//#define UPGRADE_FILE "/usr/share/deepin-authentication/modes/ofprint-serverd/ACM32FP_V30.bin"
#define UPGRADE_FILE	upgrade_new_firmware_name()
//#define FIRMWARE_VERSION_NEW	"acm32_btl_ofilm_pc_000030"
#define FIRMWARE_VERSION_NEW	upgrade_new_firmware_version()
#define FIRMWARE_VERSION_NUM_NEW	upgrade_new_firmware_version_num()
#define FILE_PACKAGE_SIZE	198816

#define WR_DELAY_MS 2

#define BOOT_DEV_NAME "usb vid=0x1234 pid=0xabce"

#define BOOT_USB_VID 0x1234
#define BOOT_USB_PID 0xabce

#define BULK_RECV_EP    0x84
#define BULK_SEND_EP    0x04
#define INT_RECV_EP     0x84
#define INT_SEND_EP     0x04

#define TRANSFER_TIMEOUT 4000

#define DOWNLOAD_DATA_LEN_MAX   240

typedef enum _DOWNLOAD_CMD_ENUM
{
    CMD_SELF_TEST = 0xC7,
    CMD_SECURITY_CODE = 0xCC,
    CMD_ERASE_EFLASH = 0xD3,
    CMD_DOWNLOAD_CODE_TO_EFLASH = 0xD7,
    CMD_CHECK_CRC = 0xD8,
    CMD_DOWNLOAD_CRC_VALUE = 0xD9,
    CMD_RUN_APP = 0xE3,
    CMD_READ_SN = 0xA0,
    CMD_READ_EFLASH = 0xD0,
    CMD_ENABLE_WRITE_PROTECT = 0xD2,
    CMD_DISABLE_WRITE_PROTECT = 0xD5,
    CMD_ENABLE_READ_PROTECT = 0xDA,
    CMD_DISABLE_READ_PROTECT = 0xD4,
    CMD_RESET = 0xCB,
    CMD_READ_CRC = 0xA6,
}DOWNLOAD_CMD_ENUM;


typedef struct {
    unsigned int pid;
    unsigned int vid;
    unsigned char bInterfaceClass;
    unsigned char bInterfaceSubClass;
    unsigned char bmAttributes;
    unsigned char bInEndpointAddress;
    unsigned char bOutEndpointAddress;
    unsigned char bInterfaceNumber;
    libusb_device *dev;
    libusb_device **devs;
} usb_dev;


#define UP_USB_BUFFER_SIZE   1024

/*协议包数据结构*/
typedef struct _UP_PROT_PACKET_
{
    uint8  prot_state;			//串口协议解析状态
    uint8  prot_head;
    uint8  prot_type;
    uint16 prot_size;
    uint8  prot_data[UP_USB_BUFFER_SIZE];  //保存上位机的参数数据
    uint32 prot_data_cnt;		//对数据长度进行计数
    uint8  prot_bcc;
    uint8  prot_end;
    //uint16 prot_checksum;

    uint8  prot_respond;		//给host的应答
    uint8  prot_source;			//数据从UART或SPI过来的,UART:0 ;SPI:1

    uint8  current_cmd_code;	//当前发送的指令码, add by hcq, 20230117
    uint8  current_resp_cmd;	//当前应答的命令
    uint8  current_resp_type;	//当前应答结果
    //uint8  recv_prot_type;
}UP_PROT_PACKET;

/*标识包处理状态机*/
enum
{
    UP_PACKET_IDLE = 0,
    UP_PACKET_HEAD,
    UP_PACKET_TYPE,
    UP_PACKET_SIZE1,
    UP_PACKET_SIZE2,
    UP_PACKET_DATA,
    UP_PACKET_BCC,
    UP_PACKET_END,
    UP_PACKET_RECV_SUCCESS,
    //PACKET_CHECKSUM1,
    //PACKET_CHECKSUM2,
    UP_PACKET_ERROR = 100,  //包出错
};

typedef struct _UPGRADE_PARAM_
{
    libusb_device_handle *usb_handle;
    usb_dev udev;

    //protocol
    UP_PROT_PACKET prot_packet_usb;	//报文协议处理结构
    uint8  usb_recv_data[UP_USB_BUFFER_SIZE];
    uint16 usb_recv_cnt;

    uint8 dev_sn[64];
    uint16 crc_value_dev;
    uint16 crc_value_file;
	uint8 upgrade_state;//升级状态，0: 未升级；1: 正在升级
    //download protocal
}UPGRADE_PARAM;


extern int32 upgrade_module_init(void);
extern int upgrade_and_run(void);
extern int check_file_exit(void);
extern int get_file_size(char *filename);
extern int read_fw_version(void);
extern char *upgrade_new_firmware_version(void);
extern char *upgrade_new_firmware_name(void);
extern uint32 upgrade_new_firmware_version_num(void);
//extern int32 rm_firmware_file(void);
extern int32 set_upgrade_state(uint8 state);
extern uint8 get_upgrade_state(void);
extern int read_fw_upgrade_flag(void);
extern int change_fw_upgrade_flag(void);





#endif //__UPGRADE_H__

